#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cthread.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    thread = new CThread(this);
    connect(thread, &CThread::threadValue, this, &MainWindow::do_thread_value);

    connect(thread, &CThread::started, this, [=](){
        ui->statusBar->showMessage("线程启动");
    });

    connect(thread, &CThread::destroyed, this, [=](){
        ui->statusBar->showMessage("线程销毁");
    });

    connect(thread, &CThread::finished, this, [=](){
        ui->statusBar->showMessage("线程结束");
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_start_clicked()
{
    thread->startThread();
}


void MainWindow::on_stop_clicked()
{
    thread->stopThread();
}

void MainWindow::do_thread_value(int step)
{
    ui->plainTextEdit->appendPlainText(QString("第%1次执行\n").arg(step));
}


void MainWindow::on_destroy_clicked()
{
    delete thread;
}

